package com.haixiaoke.saas.enterprise.dept.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.haixiaoke.saas.common.annotation.Log;
import com.haixiaoke.saas.common.annotation.TenantScope;
import com.haixiaoke.saas.common.core.domain.AjaxResult;
import com.haixiaoke.saas.common.core.page.TableDataInfo;
import com.haixiaoke.saas.common.enums.BusinessType;
import com.haixiaoke.saas.common.tenant.TenantContext;
import com.haixiaoke.saas.common.utils.poi.ExcelUtil;
import com.haixiaoke.saas.dept.domain.BizDept;
import com.haixiaoke.saas.dept.service.IBizDeptService;
import com.haixiaoke.saas.house.domain.DeptTree;
import com.haixiaoke.saas.system.BaseController;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * 企业端部门管理Controller
 *
 * @author panda
 * @date 2023-12-27
 */
@RestController
@RequestMapping("/enterprise/dept/dept")
public class EnterpriseBizDeptController extends BaseController {
    @Autowired
    private IBizDeptService bizDeptService;

    @TenantScope
    @GetMapping("/list")
    public AjaxResult list(BizDept bizDept) {
        bizDept.setTenantId(TenantContext.getTenantId());
        List<BizDept> list = bizDeptService.selectBizDeptList(bizDept);
        return success(list);
    }

    @PreAuthorize("@ss.hasPermi('enterprise:dept:list')")
    @GetMapping("/tree/list")
    @TenantScope
    public AjaxResult treeList(BizDept bizDept) {
        bizDept.setTenantId(TenantContext.getTenantId());
        List<DeptTree> list = bizDeptService.buildTreeList(bizDept);
        return success(list);
    }

    @GetMapping("/tree/addList")
    @TenantScope
    public AjaxResult addList(BizDept bizDept) {
        bizDept.setTenantId(TenantContext.getTenantId());
        List<DeptTree> list = bizDeptService.buildTreeList(bizDept);
        DeptTree node = new DeptTree();
        node.setId("0");
        node.setName("无上级");
        list.add(0,node);
        return success(list);
    }

    /**
     * 导出企业端部门管理列表
     */
    @PreAuthorize("@ss.hasPermi('enterprise:dept:export')")
    @Log(title = "企业端部门管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizDept bizDept) {
        List<BizDept> list = bizDeptService.selectBizDeptList(bizDept);
        ExcelUtil<BizDept> util = new ExcelUtil<BizDept>(BizDept.class);
        util.exportExcel(response, list, "企业端部门管理数据");
    }

    /**
     * 获取企业端部门管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('enterprise:dept:query')")
    @GetMapping(value = "/{deptId}")
    public AjaxResult getInfo(@PathVariable("deptId") Long deptId) {
        return success(bizDeptService.selectBizDeptByDeptId(deptId));
    }

    /**
     * 新增企业端部门管理
     */
    @PreAuthorize("@ss.hasPermi('enterprise:dept:add')")
    @Log(title = "企业端部门管理", businessType = BusinessType.INSERT)
    @PostMapping
    @TenantScope
    public AjaxResult add(@RequestBody BizDept bizDept) {
        bizDept.setCreateBy(getUsername());
        bizDept.setTenantId(TenantContext.getTenantId());
        return toAjax(bizDeptService.insertBizDept(bizDept));
    }

    /**
     * 修改企业端部门管理
     */
    @PreAuthorize("@ss.hasPermi('enterprise:dept:edit')")
    @Log(title = "企业端部门管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizDept bizDept) {
        bizDept.setUpdateBy(getUsername());
        return toAjax(bizDeptService.updateBizDept(bizDept));
    }

    /**
     * 删除企业端部门管理
     */
    @PreAuthorize("@ss.hasPermi('enterprise:dept:remove')")
    @Log(title = "企业端部门管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{deptId}")
    public AjaxResult remove(@PathVariable("deptId") Long deptId) {
        if (bizDeptService.hasChildByDeptId(deptId)) {
            return warn("部门已创建下级部门，不能删除");
        }
        if (bizDeptService.checkDeptExistUser(deptId)) {
            return warn("部门已关联企业用户，不能删除");
        }
        return toAjax(bizDeptService.deleteBizDeptByDeptId(deptId));
    }
}
